package com.lxh.spring.dao;


import com.lxh.spring.model.User;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 1506101101-罗兴华
 * @version 2017-10-29 10:35
 */
@Repository("jdbcUserDao")
public class UserDao implements IUserDao {

    private JdbcTemplate jdbcTemplate;


    @Resource
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public void add(User user) {
        this.jdbcTemplate.update(
                "insert into user (userId,userName, userPassword,nickname,mail) values (?,?,?,?,?)",
                user.getId(),user.getUsername(), user.getPassword(), user.getNickname(),user.getMail());

    }

    @Override
    public void update(User user) {
        this.jdbcTemplate.update(
                "update user set userName = ?,userPassword=?,nickname=?,mail=?  where userid = ?",
                user.getUsername(), user.getPassword(), user.getNickname(),user.getMail(), user.getId());

    }

    @Override
    public void delete(int id) {
        this.jdbcTemplate.update("delete from t_user where id = ?", id);
    }

    @Override
    public Map<String, Object> load(int id) {
        Map<String, Object> map=new HashMap<String, Object>();
        Object[] params = new Object[] {id};
        return this.jdbcTemplate.queryForObject("select * from user where userId=?",params,new ColumnMapRowMapper());
    }

    @Override
    public List<Map<String, Object>> list() {

        return  this.jdbcTemplate.query("select * from user",new ColumnMapRowMapper());
    }


}
